package com.hunan.api.catv.service.biz.interactive;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.dto.PlatinumIslandInfoDto;
import com.hunan.api.catv.entity.po.MemberAccountRelated;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.system.SystemDictBiz;
import com.hunan.api.catv.utils.HttpUtils;
import com.hunan.api.catv.utils.SignMd5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wangchao
 * @Date: 2021/2/1 14:12
 */
@Service
public class InteractiveBiz extends ResultService {

    public static Logger log = LoggerFactory.getLogger(InteractiveBiz.class);


    @Autowired
    SystemDictBiz systemDictBiz;


    /**
     * 通过完成任务新增白金岛金币
     * type 1:登录 2:分享 3:助力
     */
    public ServiceResult addGoldToByPlatinumIslandTask(Integer type, Integer memberId) throws NoSuchAlgorithmException {
        //查询该用户是否注册白金岛账号
        MemberAccountRelated memberAccountRelated = new MemberAccountRelated().selectOne(new QueryWrapper<MemberAccountRelated>().eq(MemberAccountRelated.MEMBER_ID, memberId));
        if (memberAccountRelated == null) {
            return buildFail("该用户尚未绑定TV盒子");
        }
        PlatinumIslandInfoDto platinumIslandInfoDto = JSONUtil.toBean(systemDictBiz.getStringByKey(Constants.PLATINUM_ISLAND_CONF), PlatinumIslandInfoDto.class);
        //白金岛账号id
        String accountId = memberAccountRelated.getAccountId();
        //获取当前时间戳
        long currentTimeMillis = System.currentTimeMillis();
        //获取签名串
        String signStr = accountId + type + currentTimeMillis + platinumIslandInfoDto.getSignatureKey();
        String sign = SignMd5Utils.toMd5(signStr);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("account", accountId);
        dataMap.put("type", type);
        dataMap.put("t", currentTimeMillis);
        dataMap.put("sign", sign);
        JSONObject jsonObject = null;
        try {
            jsonObject = HttpUtils.httpPostWithoutJson(platinumIslandInfoDto.getAddGoldUrl(), dataMap);
            if (!"0".equals(jsonObject.getString("code"))) {
                return buildFail(jsonObject.getString("msg"));
            }
        } catch (Exception e) {
            log.error("完成任务新增白金币错误:{}", e);
        }
        return buildOK(jsonObject);
    }


    /**
     * 获取该用户白金岛战绩以及排行榜
     */
    public ServiceResult getRanklist(Integer limit, Integer memberId) {
        //查询该用户是否注册白金岛账号
        MemberAccountRelated memberAccountRelated = new MemberAccountRelated().selectOne(new QueryWrapper<MemberAccountRelated>().eq(MemberAccountRelated.MEMBER_ID, memberId));
        if (memberAccountRelated == null) {
            return buildFail("该用户尚未绑定TV盒子");
        }
        PlatinumIslandInfoDto platinumIslandInfoDto = JSONUtil.toBean(systemDictBiz.getStringByKey(Constants.PLATINUM_ISLAND_CONF), PlatinumIslandInfoDto.class);
        //白金岛账号id
        String accountId = memberAccountRelated.getAccountId();
        StringBuilder builder = new StringBuilder();
        builder.append(platinumIslandInfoDto.getRanklistsUrl());
        builder.append("?");
        builder.append("limit=").append(limit);
        builder.append("&account=").append(accountId);
        JSONObject jsonObject = null;
        try {
            jsonObject = HttpUtils.retryHttpGet(builder.toString(), 3);
            if (!"0".equals(jsonObject.getString("code"))) {
                return buildFail(jsonObject.getString("msg"));
            }
        } catch (Exception e) {
            log.error("获取排行榜列表失败:{}", e);
        }
        return buildOK(jsonObject);
    }
}
